home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
public
/
bit
/
src
/
ulib
/
time.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
4KB
|
155 lines
/***********************************************************************
* $Id: time.c,v 0.80 1994/02/24 09:48:11 zhao Exp $
*
*. Copyright(c) 1993,1994 by T.C. Zhao
* All rights reserved.
*.
*
* Simple (real) timing routine. The time itself has no meaning to
* the caller, but a diff between two calls does.
*
***********************************************************************/
#if !defined(lint) && defined(F_ID)
char *id_tim = "$Id: time.c,v 0.80 1994/02/24 09:48:11 zhao Exp $";
#endif
#include "ulib.h"
#include <sys/time.h>
static long lsec, lusec;
static struct timeval now;
void
reset_time(void)
{
gettimeofday(&now, 0);
lsec = now.tv_sec;
lusec = now.tv_usec;
}
/*************************************************************
* time passed in milli-second since last reset. Can't be longer
* than a few hundred hours due to overflows, but for all pratical
* purposes of measuring time difference, it is fine.
*************************************************************/
long
time_passed(void)
{
gettimeofday(&now, 0);
return (now.tv_sec - lsec) * 1000 + (now.tv_usec - lusec) / 1000;
}
double
current_time(void)
{
gettimeofday(&now, 0);
return ((double) now.tv_sec + (double) 0.000001 * now.tv_usec);
}
/************ Current date in ASCII *****************/
#include <time.h>
const char *
current_ascii_date(void)
{
time_t t = time(0);
return asctime(localtime(&t));
}
/********************************************************************
* Given hour, min , day, month (0-11),
* and year return Unix time. The only thing we need is the start
* time of the month and year.
*******************************************************************/
static time_t
date2time(int mins, int hours, int days, int month, int year, int gmt)
{
time_t min, hr, day;
long guess;
struct tm *t;
int delta;
min = 60;
hr = 60 * min;
day = 24 * hr;
if (year >= 1900)
year -= 1900;
guess = time(0); /* get current time */
t = (gmt ? gmtime : localtime) (&guess);
while ((delta = year - t->tm_year))
{
guess += delta * (364 * day);
t = (gmt ? gmtime : localtime) (&guess);
}
while ((delta = month - t->tm_mon))
{
guess += delta * (28 * day);
t = (gmt ? gmtime : localtime) (&guess);
}
/* this will be the time at the start of the month */
guess -= t->tm_sec + t->tm_min * min + t->tm_hour * hr +
(t->tm_mday - 1) * day;
guess += (days - 1) * day + mins * min + hours * hr;
return guess;
}
#if 0
/****************************************************************
* Convert an ascii month to ordinal
****************************************************************/
static const char *months[] =
{
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
static int
month_to_num(const char *month)
{
int i;
for (i = 1; i <= 12 && strcasecmp(months[i], month); i++)
;
return (i % 13);
}
#endif
/*****************************************************************
* Given an output of date, convert to unix time.
* the date is generated by date '+%m %e %Y %H' for
* country-independence
*****************************************************************/
time_t
date_to_time(const char *date)
{
static char mon[10] = "02", ahr[10] = "1";
int year = 1994, month = 2, day = 30, hr = 20;
sscanf(date, "%s %d %d %s", mon, &day, &year, ahr);
/* need to nuke the 0 in months and hr */
month = atol(mon + (mon[0] == 0));
hr = atol(ahr + (ahr[0] == 0));
/* note month starts from 0! for date2time */
return date2time(0, hr, day, month - 1, year, 0);
}
#ifdef TEST
#include <stdio.h>
main()
{
char aa[1024];
fprintf(stderr, "current date: %s\n", current_ascii_date());
fprintf(stderr, "current time: %g\n", current_time());
fprintf(stderr, "current time: %ld\n", (long) time(0));
fprintf(stderr, "IN m e y t");
while (gets(aa))
fprintf(stderr, "%s=>%ld\n", aa, (long) date_to_time(aa, 1));
}
#endif